from toee import *

from __main__ import game

import _include ## Neccessary for timedEventAdd to work across saves and loads - CtB



# function: party_transfer_to( target, oname )
# arguments: target - PyObjHandle (the recepient of the item)
#            oname - the internal name of the object to be trasnfered
#                    from the party
# returns: the PyObjHandle of the object found, OBJ_HANDLE_NULL if not found
def party_transfer_to( target, oname ):
	for pc in game.party:
		item = pc.item_find( oname )
		if item != OBJ_HANDLE_NULL:
			pc.item_transfer_to( target, oname )
			return item
	return OBJ_HANDLE_NULL

def find_npc_near( obj, name ):
	for npc in game.obj_list_vicinity( obj.location, OLC_NPC ):
		if (npc.name == name):
			return npc
	return OBJ_HANDLE_NULL

def find_container_near( obj, name ):
	for container in game.obj_list_vicinity( obj.location, OLC_CONTAINER ):
		if (container.name == name):
			return container
	return OBJ_HANDLE_NULL

def group_average_level( pc ):
	count = 0
	level = 0
	for obj in pc.group_list():
		count = count + 1
		level = level + obj.stat_level_get( stat_level )
	if (count == 0):
		return 1
####Old method###############
	# level = level + ( count / 2 )
	# avg = level / count
#########################
	avg = (level/count) + (count - 4)/2
	return avg

def group_wilderness_lore( pc ):
	high = 0
	level = 0
	for obj in pc.group_list():
		level = obj.skill_level_get( skill_wilderness_lore )
		if (level > high):
			high = level
	if (high == 0):
		return 1
	return high

def obj_percent_hp( obj ):
	curr = obj.stat_level_get( stat_hp_current )
	max = obj.stat_level_get( stat_hp_max )
	if (max == 0):
		return 100
	if (curr > max):
		curr = max
	if (curr < 0):
		curr = 0
	percent = (curr * 100) / max
	return percent

def group_percent_hp( pc ):
	percent = 0
	cnt = 0
	for obj in pc.group_list():
		percent = percent + obj_percent_hp(obj)
		cnt = cnt + 1
	if (cnt == 0):
		percent = 100
	elif (percent < 0):
		percent = 0
	else:
		percent = percent / cnt
	return percent

def group_pc_percent_hp( attachee,pc ):
	percent = 0
	cnt = 0
	for obj in pc.group_list():
		if (obj != attachee):
			percent = percent + obj_percent_hp(obj)
			cnt = cnt + 1
	if (cnt == 0):
		percent = 100
	elif (percent < 0):
		percent = 0
	else:
		percent = percent / cnt
	return percent

def group_kobort_percent_hp( attachee,pc ):
	percent = 0
	cnt = 0
	for obj in pc.group_list():
		if (obj != attachee and obj.name != 8005):
			percent = percent + obj_percent_hp(obj)
			cnt = cnt + 1
	if (cnt == 0):
		percent = 100
	elif (percent < 0):
		percent = 0
	else:
		percent = percent / cnt
	return percent


def create_item_in_inventory( item_proto_num, npc ):
	item = game.obj_create(item_proto_num, npc.location)
	if (item != OBJ_HANDLE_NULL):
		npc.item_get(item)
	return item

def is_daytime():
	return game.is_daytime()


def is_safe_to_talk(speaker,listener):
	if (speaker.can_see(listener)):
		if (speaker.distance_to(listener) <= 15):
			return 1
	return 0

def start_game_with_quest(quest_number):
	game.quests[quest_number].state = qs_accepted
	# hommlet movies start at 1009 and go to 1017
	# but the quests start at 22, so offset by 987
	game.fade_and_teleport(0,0,987 + quest_number,5001,711,521)
	return

def start_game_with_botched_quest(quest_number):
	game.quests[quest_number].state = qs_mentioned
	game.quests[quest_number].state = qs_botched
	# hommlet no-voice movies start at 1018 and go to 1026
	# but the quests start at 22, so offset by 996
	game.fade_and_teleport(0,0,996 + quest_number,5001,711,521)
	return


def critter_is_unconscious(npc):
	curr = npc.stat_level_get( stat_hp_current )
	if (curr < 0):
		return 1
	if (npc.stat_level_get(stat_subdual_damage) > curr):
		return 1
	return 0

# HTN - returns true if obj is an "item" (obj.h)
def obj_is_item( obj ):

	return (obj.type == obj_t_projectile) or (obj.type == obj_t_weapon) or (obj.type == obj_t_ammo) or (obj.type == obj_t_armor) or (obj.type == obj_t_scroll) or (obj.type == obj_t_bag)

# HTN - compares two OBJ_HANDLE's by "hit_dice", used for sorting OBJ_HANDLE lists
def obj_comparison_by_hit_dice( obj_1, obj_2 ):

	obj_1_hitdice = obj_1.hit_dice
	obj_2_hitdice = obj_2.hit_dice

	return cmp( obj_1_hitdice, obj_2_hitdice )

def pyspell_targetarray_copy_to_obj_list( spell ):
	copy_list = []
	for item in spell.target_list:
		copy_list.append( item.obj )
	return copy_list

# Function: location_to_axis( PyLong loc )
# Author  : Steve Moret
# Returns : A tuple of two PyInts
# Purpose : returns an x, y for a specified location
#           can be used like:  
#  x, y = location_to_axis( npc.location )
#
def location_to_axis( loc ):
	y = loc >> 32
	x = loc & 4294967295
	return ( x, y )

# Function: location_from_axis( PyInt x, PyInt y )
# Author  : Steve Moret
# Returns : A PyLong representing the location
# Purpose : returns a location represented by the x, and y components x,y
#
def location_from_axis( x, y ):
	# initialize loc to be a LONG integer
	loc = 0L + y
	loc = ( loc << 32 ) + x
	return loc

# Function: set_end_slides( npc, pc )
# Author  : Tom Decker
# Returns : nada
# Purpose : queues up all the end slides for the end game
#
def	set_end_slides( attachee, triggerer ):
	if game.global_flags[189] == 1:
		if game.global_flags[183] == 1:
			game.moviequeue_add(204)
		elif game.global_flags[326] == 0:
			game.moviequeue_add(200)
		else:
			game.moviequeue_add(201)
	elif game.global_flags[188] == 1:
		game.moviequeue_add(202)
	elif game.global_flags[326] == 1:
		game.moviequeue_add(203)
	elif game.global_flags[186] == 1:
		game.moviequeue_add(205)
	elif game.global_flags[187] == 1:
		game.moviequeue_add(206)
	elif game.global_flags[184] == 1:
		game.moviequeue_add(207)
	elif ( game.global_flags[190] == 1 ) or ( game.global_flags[191] == 1 ):
		game.moviequeue_add(208)
	if game.global_flags[327] == 1:
		game.moviequeue_add(209)
	elif game.global_flags[326] == 1:
		game.moviequeue_add(211)
	if game.global_flags[328] == 1:
		game.moviequeue_add(210)
	if game.global_flags[37] == 0 and ( not(anyone( triggerer.group_list(), "has_follower", 8002 )) ):
		game.moviequeue_add(212)
	elif game.global_flags[37] == 1:
		game.moviequeue_add(213)
	if game.global_flags[150] == 1:
		game.moviequeue_add(215)
	elif ( game.global_flags[151] == 1 ) or ( game.global_flags[152] == 1 ) or ( anyone( triggerer.group_list(), "has_follower", 8031 ) ):
		game.moviequeue_add(214)
	else:
		game.moviequeue_add(216)
	if game.global_vars[29] >= 10:
		game.moviequeue_add(217)
	if ( game.global_flags[90] == 1 and game.global_flags[324] == 1 and game.global_flags[365] == 0 ):
		game.moviequeue_add(218)
	if ( game.quests[12].state == qs_completed and game.global_flags[364] == 0 ):
		game.moviequeue_add(219)
	if ( game.quests[26].state == qs_completed and ( game.global_flags[189] == 1 or game.global_flags[188] == 1 ) ):
		game.moviequeue_add(220)
	if game.quests[24].state == qs_completed:
		game.moviequeue_add(221)
	if game.global_flags[61] == 1:
		game.moviequeue_add(222)
	if ( game.global_flags[97] == 1 and game.global_flags[329] == 1 ):
		game.moviequeue_add(223)
	if ( game.global_flags[146] == 1 and game.global_flags[104] == 1 and game.global_flags[105] == 1 and game.global_flags[106] == 1 and game.global_flags[107] == 1 ):
		game.moviequeue_add(224)
	if game.global_flags[147] == 0:
		game.moviequeue_add(225)
	if game.global_flags[177] == 0:
		game.moviequeue_add(226)
	if ( game.global_flags[68] == 1 and game.global_flags[331] == 0 ):
		game.moviequeue_add(230)
	if ( game.global_flags[83] == 1 and game.global_flags[330] == 0 ):
		game.moviequeue_add(231)
	if ( game.global_flags[46] == 1 and game.global_flags[196] == 0 and game.global_flags[318] == 0 ):
		if game.global_flags[332] == 1:
			game.moviequeue_add(232)
		else:
			game.moviequeue_add(233)
	if ( game.quests[6].state == qs_completed and game.global_flags[333] == 0 and game.global_flags[334] == 0 ):
		if game.global_flags[332] == 1:
			game.moviequeue_add(234)
		else:
			game.moviequeue_add(235)
	if game.global_flags[146] == 0:
		game.moviequeue_add(236)
	if game.global_flags[105] == 0:
		game.moviequeue_add(237)
	if game.global_flags[104] == 0:
		game.moviequeue_add(238)
	if game.global_flags[106] == 0:
		game.moviequeue_add(239)
	if game.global_flags[107] == 0:
		game.moviequeue_add(240)
	if game.global_flags[335] == 0:
		game.moviequeue_add(241)
	if ( game.quests[15].state != qs_completed and game.global_flags[336] == 0 ):
		game.moviequeue_add(242)
	if ( game.global_flags[299] == 0 and game.global_flags[337] == 0 and game.global_flags[336] == 0 and ( game.global_flags[189] == 1 or game.global_flags[188] == 1 ) ):
		game.moviequeue_add(243)
	elif ( ( game.global_flags[299] == 1 or game.global_flags[337] == 1 or game.global_flags[336] == 1 ) and ( game.global_flags[189] == 1 or game.global_flags[188] == 1 ) ):
		game.moviequeue_add(244)
	elif ( game.global_flags[186] == 1 or game.global_flags[187] == 1 or game.global_flags[184] == 1 or game.global_flags[190] == 1 or game.global_flags[191] == 1 ):
		game.moviequeue_add(245)
	if ( game.global_flags[186] == 1 or game.global_flags[184] == 1 or game.global_flags[190] == 1 or game.global_flags[191] == 1 ):
		game.moviequeue_add(246)
	elif game.global_flags[187] == 1:
		game.moviequeue_add(247)
	elif ( game.global_flags[189] == 1 or game.global_flags[188] == 1 ):
		game.moviequeue_add(248)
	if game.global_flags[338] == 0:
		game.moviequeue_add(249)
	if game.quests[20].state == qs_completed:
		game.moviequeue_add(250)
	if game.global_flags[339] == 1:
		game.moviequeue_add(251)
	if ( game.quests[23].state == qs_completed and game.global_flags[306] == 1 ):
		game.moviequeue_add(252)
	if ( game.quests[25].state == qs_completed and ( game.global_flags[189] == 1 or game.global_flags[188] == 1 ) ):
		game.moviequeue_add(253)
	if game.quests[27].state == qs_completed:
		game.moviequeue_add(254)
	if ( anyone( triggerer.group_list(), "has_follower", 8031 ) and ( game.global_flags[189] == 1 or game.global_flags[188] == 1 ) ):
		if (game.party_alignment == LAWFUL_EVIL):
			game.moviequeue_add(262)
			game.moviequeue_add(255)
		else:
			game.moviequeue_add(261)
	elif (game.party_alignment == LAWFUL_EVIL):
		if (game.quests[28].state == qs_completed):
			game.moviequeue_add(255)
		else:
			game.moviequeue_add(256)
	if ( game.quests[29].state == qs_completed and game.global_flags[190] == 0 and game.global_flags[191] == 0 ):
		game.moviequeue_add(257)
	if ( game.quests[30].state == qs_completed and game.global_flags[190] == 0 and game.global_flags[191] == 0 ):
		game.moviequeue_add(258)
	if ( anyone( triggerer.group_list(), "has_follower", 8014 ) and ( game.global_flags[189] == 1 or game.global_flags[188] == 1 ) ):
		game.moviequeue_add(259)
	if ( anyone( triggerer.group_list(), "has_follower", 8021 ) and ( game.global_flags[189] == 1 or game.global_flags[188] == 1 ) ):
		game.moviequeue_add(260)
	if ( anyone( triggerer.group_list(), "has_follower", 8060 ) and ( game.global_flags[189] == 1 or game.global_flags[188] == 1 ) ):
		game.moviequeue_add(263)
	if ( anyone( triggerer.group_list(), "has_follower", 8061 ) and anyone( triggerer.group_list(), "has_follower", 8029 ) and anyone( triggerer.group_list(), "has_follower", 8030 ) and ( game.global_flags[189] == 1 or game.global_flags[188] == 1 or game.global_flags[185] == 1) ):
		game.moviequeue_add(264)
	if ( anyone( triggerer.group_list(), "has_follower", 8023 ) and ( game.global_flags[189] == 1 or game.global_flags[188] == 1 or game.global_flags[185] == 1) ):
		game.moviequeue_add(265)
	if ( anyone( triggerer.group_list(), "has_follower", 8023 ) and ( game.global_flags[189] == 1 or game.global_flags[188] == 1 or game.global_flags[185] == 1) ):
		game.moviequeue_add(266)
	if ( anyone( triggerer.group_list(), "has_follower", 8062 ) and ( game.global_flags[189] == 1 or game.global_flags[188] == 1 or game.global_flags[185] == 1) ):
		game.moviequeue_add(267)
	return

# Function: set_join_slides( npc, pc )
# Author  : Tom Decker
# Returns : nada
# Purpose : queues up the end slides if you join the temple
#
def	set_join_slides( attachee, triggerer ):
	if game.global_flags[327] == 1:
		game.moviequeue_add(209)
	elif game.global_flags[326] == 1:
		game.moviequeue_add(211)
	if game.global_flags[328] == 1:
		game.moviequeue_add(210)
	if game.global_flags[37] == 0 and ( not(anyone( triggerer.group_list(), "has_follower", 8002 )) ):
		game.moviequeue_add(212)
	elif game.global_flags[37] == 1:
		game.moviequeue_add(213)
	if game.global_flags[150] == 1:
		game.moviequeue_add(215)
	if game.global_vars[29] >= 10:
		game.moviequeue_add(217)
	if ( game.global_flags[90] == 1 and game.global_flags[324] == 1 and game.global_flags[365] == 0 ):
		game.moviequeue_add(218)
	if ( game.quests[12].state == qs_completed and game.global_flags[364] == 0 ):
		game.moviequeue_add(219)
	if ( game.quests[26].state == qs_completed and ( game.global_flags[189] == 1 or game.global_flags[188] == 1 ) ):
		game.moviequeue_add(220)
	if game.quests[24].state == qs_completed:
		game.moviequeue_add(221)
	if game.global_flags[61] == 1:
		game.moviequeue_add(222)
	if ( game.global_flags[97] == 1 and game.global_flags[329] == 1 ):
		game.moviequeue_add(223)
	if ( game.global_flags[68] == 1 and game.global_flags[331] == 0 ):
		game.moviequeue_add(230)
	if ( game.global_flags[83] == 1 and game.global_flags[330] == 0 ):
		game.moviequeue_add(231)
	if ( game.global_flags[46] == 1 and game.global_flags[196] == 0 and game.global_flags[318] == 0 ):
		if game.global_flags[332] == 1:
			game.moviequeue_add(232)
		else:
			game.moviequeue_add(233)
	if ( game.quests[6].state == qs_completed and game.global_flags[333] == 0 and game.global_flags[334] == 0 ):
		if game.global_flags[332] == 1:
			game.moviequeue_add(234)
		else:
			game.moviequeue_add(235)
	if ( game.quests[15].state != qs_completed and game.global_flags[336] == 0 ):
		game.moviequeue_add(242)
	game.moviequeue_add(245)
	game.moviequeue_add(246)
	if game.quests[20].state == qs_completed:
		game.moviequeue_add(250)
	if game.global_flags[339] == 1:
		game.moviequeue_add(251)
	if ( game.quests[23].state == qs_completed and game.global_flags[306] == 1 ):
		game.moviequeue_add(252)
	if ( game.quests[25].state == qs_completed and ( game.global_flags[189] == 1 or game.global_flags[188] == 1 ) ):
		game.moviequeue_add(253)
	if game.quests[27].state == qs_completed:
		game.moviequeue_add(254)
	if (game.party_alignment == LAWFUL_EVIL):
		if (game.quests[28].state == qs_completed):
			game.moviequeue_add(255)
		else:
			game.moviequeue_add(256)
	if ( game.quests[29].state == qs_completed and game.global_flags[190] == 0 and game.global_flags[191] == 0 ):
		game.moviequeue_add(257)
	if ( game.quests[30].state == qs_completed and game.global_flags[190] == 0 and game.global_flags[191] == 0 ):
		game.moviequeue_add(258)
	game.moviequeue_add(206)	
	return

# Function: should_heal_hp_on( obj )
# Author  : Tom Decker
# Returns : 1 if character is not at full health, else 0
# Purpose : to heal only characters that need it
#
def should_heal_hp_on( obj ):
	cur = obj.stat_level_get( stat_hp_current )
	max = obj.stat_level_get( stat_hp_max )
	if (not cur == max) and (obj.stat_level_get(stat_hp_current) >= -9):
		return 1
	return 0

# Function: should_heal_disease_on( obj )
# Author  : Tom Decker
# Returns : 1 if obj is diseased, else 0
# Purpose : to heal only characters that need it
#
def should_heal_disease_on( obj ):
    # check if obj is diseased
	if (obj.stat_level_get(stat_hp_current) >= -9):
		return 1
	return 0

# Function: should_heal_poison_on( obj )
# Author  : Tom Decker
# Returns : 1 if obj is poisoned, else 0
# Purpose : to heal only characters that need it
#
def should_heal_poison_on( obj ):
	# check if obj has poison on them
	if (obj.stat_level_get(stat_hp_current) >= -9):
		return 1
	return 0

# Function: should_resurrect_on( obj )
# Author  : Tom Decker
# Returns : 1 if obj is dead, else 0
# Purpose : to heal only characters that need it
#
def should_resurrect_on( obj ):
	if (obj.stat_level_get(stat_hp_current) <= -10):
		return 1
	return 0

def zap( attachee, triggerer ):
	damage_dice = dice_new( '2d4' )
	game.particles( 'sp-Shocking Grasp', triggerer )
	if triggerer.reflex_save_and_damage( OBJ_HANDLE_NULL, 20, D20_Save_Reduction_Half, D20STD_F_NONE, damage_dice, D20DT_ELECTRICITY, D20DAP_UNSPECIFIED, 0 , D20DAP_NORMAL ):
#		saving throw successful
		triggerer.float_mesfile_line( 'mes\\spell.mes', 30001 )
	else:
#		saving throw unsuccessful
		triggerer.float_mesfile_line( 'mes\\spell.mes', 30002 )

	return RUN_DEFAULT

def make_fallen():
	for pc in game.party:
		if pc.type == obj_t_pc and pc.stat_level_get():
			pc.condition_add_with_args("Fallen_Paladin",0,0)
	

